﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;
using Ivvy_Silverlight.i7SR;
using Serialization;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using Infragistics.Controls.Schedules;
using Infragistics.Controls.Interactions;
using Infragistics.Controls.Interactions.Primitives;
using Infragistics.Controls.Schedules.Primitives;

namespace Ivvy_Silverlight.Ivvy
{    
     
    public class SaveLoad
    {
        public static string IvvySilverlightFileNameinIsolatedStorage = "Ivvy_Silverlight_File_Name_in_IsolatedStorage";
        public static string IvvySilverlightFileNameinIsolatedStorageBinary = "Ivvy_Silverlight_File_Name_in_IsolatedStorage_Binary";
        public static Int64 QuotaOfIsolatedStorage = 1024 * 1024 * 1200;
        private IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;

        public void ClearStore()
        {
            iss.Clear();
        }
        public void AutoSave()
        {
            iss.Save();
        }
        public string Save(ClientData Store)
        {
            string ReturnKey=Guid.NewGuid().ToString();
            iss.Add(ReturnKey, Store);
            return ReturnKey;
        }
        public string Save(XamMonthView Store)
        {
            string ReturnKey = Guid.NewGuid().ToString();
            iss.Add(ReturnKey, Store);
            return ReturnKey;
        }
        public ClientData Load(string ReturnKey)
        {
            ClientData rt = new ClientData();
                if (iss.Contains(ReturnKey))
                {
                     return (ClientData)iss[ReturnKey];
                }

                return rt;
        }
        public XamMonthView Load_XamMonthView(string ReturnKey)
        {
            XamMonthView rt = new XamMonthView();
            if (iss.Contains(ReturnKey))
            {
                return (XamMonthView)iss[ReturnKey];
            }

            return rt;
        }

        public static byte[] SaveToBinary(object ObjectToSerialize)
        {
            return SilverlightSerializer.Serialize(ObjectToSerialize);                        
        }//public byte[] SaveToBinary;            

        public static object LoadFromBinary(byte[] ObjectToDeSerialize)
        {
            return SilverlightSerializer.Deserialize(ObjectToDeSerialize);
        }//public byte[] LoadFromBinary;            

        public static string SaveToXMLData(object ObjectToSerialize)
        {
            XmlSerializer sr = new XmlSerializer(ObjectToSerialize.GetType());
            StringBuilder sb = new StringBuilder();
            StringWriter w = new StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture);
            sr.Serialize(w, ObjectToSerialize);
            return sb.ToString();
        }//public string SaveToXMLData()};            
        
        public static object LoadFromXML(string XMLForDeserialization, System.Type TypeToLoad)
        {
            StringReader reader = new StringReader(XMLForDeserialization);
            XmlSerializer dsr = new XmlSerializer(TypeToLoad);            
            return dsr.Deserialize(reader);
        }//        public void LoadFromXML(string XMLForDeserialization)

        public static void SaveBinaryDataToLocalStorage(byte[] BinData, string fileName)
        {
            SaveLoad.IncreaseQuotaOfIsolatedStorage(QuotaOfIsolatedStorage);            
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {                
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
                {
                    using (BinaryWriter BinWriter = new BinaryWriter(isfs))
                    {
                        BinWriter.Write(BinData, 0, BinData.Length);
                        BinWriter.Close();
                    }//using (StreamWriter sw = new StreamWriter(isfs))
                }//using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
            }//using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        }//public static void SaveBinaryDataToLocalStorage(byte[] BinData, string fileName)

        public static byte[] LoadBinaryDataFromLocalStorage(string fileName)
        {
            byte[] data;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
                {                    
                    using (BinaryReader BinReader = new BinaryReader(isfs))
                    {
                        data = BinReader.ReadBytes(Convert.ToInt32(isfs.Length));
                        BinReader.Close();                        
                    }//using (StreamReader sr = new StreamReader(isfs))
                }//using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
            }//using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            return data;
        }//public static byte[] LoadBinaryDataFromLocalStorage(string fileName)


        public static void SaveStringDataToLocalStorage(string data, string fileName)
        {
            SaveLoad.IncreaseQuotaOfIsolatedStorage(QuotaOfIsolatedStorage);
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
                {
                    using (StreamWriter sw = new StreamWriter(isfs))
                    {
                        sw.Write(data);
                        sw.Close();
                    }//using (StreamWriter sw = new StreamWriter(isfs))
                }//using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
            }//using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        }//public static void SaveStringDataToLocalStorage(string data, string fileName)

        public static string LoadStringDataFromLocalStorage(string fileName)
        {
            string data = String.Empty;
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
                {
                    using (StreamReader sr = new StreamReader(isfs))
                    {
                        data = sr.ReadToEnd();                        
                    }//using (StreamReader sr = new StreamReader(isfs))
                }//using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
            }//using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            return data;
        }//public static string LoadStringDataFromLocalStorage(string fileName)

        public static bool IncreaseQuotaOfIsolatedStorage(Int64 NewSize)
        {
            bool Result = false;
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    Int64 curAvail = store.Quota;                    
                    if (curAvail < NewSize)                    
                        Result = store.IncreaseQuotaTo(NewSize);
                }
            }
            catch (IsolatedStorageException)
            {               

            }
            return Result;
        }//public static bool IncreaseQuotaOfIsolatedStorage(Int64 NewSize)

    }//public class SaveLoad
}//namespace Ivvy_Silverlight.Ivvy
